package com.wpen.unit.component;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ValidatorUtil {
	@Autowired
	ValidatorFactory validatorFactory;

	public void validate(Object object) {
		Validator validator = validatorFactory.getValidator();
		// org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider550
		// 580
		Set<ConstraintViolation<Object>> set = validator.validate(object);
		if (set != null && !set.isEmpty()) {
			StringBuffer sBuffer = new StringBuffer();
			for (ConstraintViolation<Object> constraintViolation : set) {
				sBuffer.append(constraintViolation.getPropertyPath()).append(constraintViolation.getMessage())
						.append(";");
			}
			throw new ValidationException(sBuffer.toString());
		}
	}

}
